package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.QuestionChose;
import com.chzu.pojo.QuestionInfo;
import com.chzu.result.JsonResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.QuestionChoseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName QuestionChoseController
 * @Description TODO
 * @Date 2022/02/21 19:23
 */
@RestController
@RequestMapping("/questionChose")
@Api(tags = "题目选项的接口")
public class QuestionChoseController {

    @Resource
    private QuestionChoseService questionChoseService;

    @ApiOperation("查询所有试题选项")
    @PostMapping("/questionChosePage")
    public JsonResult<Object> getAllQuestionChose(@RequestBody QueryInfo queryInfo) {
        return questionChoseService.getAllQuestionChose(queryInfo);
    }

    @ApiOperation("添加某个试题的选项")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/")
    public JsonResult<String> addQuestionChose(@RequestBody QuestionChose questionChose) {
        // 选项自动转大写
        questionChose.setChoseId(Character.toUpperCase(questionChose.getChoseId()));
        boolean save = questionChoseService.save(questionChose);
        if (!save) {
            return JsonResult.fail("添加试题选项失败");
        }
        return JsonResult.ok("添加试题选项成功");
    }

    @ApiOperation("修改某个试题选项的信息")
    @Transactional(rollbackFor= Exception.class)
    @PutMapping("/")
    public JsonResult<String> putQuestionChose(@RequestBody QuestionChose questionChose) {
        // 返回受影响的行数
        int res = questionChoseService.putQuestionChose(questionChose);
        if (res <= 0) {
            return JsonResult.fail("修改试题选项失败");
        }
        return JsonResult.ok("修改试题选项成功");
    }

    @ApiOperation("删除某个试题选项")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/{questionId}")
    public JsonResult<String> deleteQuestionChose(@PathVariable("questionId") String questionId) {
        QueryWrapper<QuestionChose> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",questionId);
        boolean result = questionChoseService.remove(wrapper);
        if (!result) {
            return JsonResult.fail("删除试题选项失败");
        }
        return JsonResult.ok("删除试题选项成功");
    }

}
